package cn.com.homeyoda.aimate.aihealth.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.camera.view.PreviewView;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.TextureView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.view.PreviewView;
import androidx.constraintlayout.widget.ConstraintLayout;

public class CircularCameraView extends ConstraintLayout {
    private PreviewView previewView;
    private Paint maskPaint;
    private Path circlePath;
    private Paint borderPaint;
    private int borderWidth = 5;
    private int borderColor = Color.WHITE;

    public CircularCameraView(@NonNull Context context) {
        super(context);
        init();
    }

    public CircularCameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CircularCameraView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 必须禁用硬件加速才能正确裁剪
        setLayerType(LAYER_TYPE_SOFTWARE, null);

        // 初始化PreviewView
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            previewView = new PreviewView(getContext());
        }
        previewView.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            previewView.setScaleType(PreviewView.ScaleType.FILL_CENTER);
        }
        addView(previewView);

        // 初始化圆形遮罩
        maskPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        maskPaint.setColor(Color.WHITE);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));

        circlePath = new Path();

        // 初始化边框
        borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        borderPaint.setStyle(Paint.Style.STROKE);
        borderPaint.setStrokeWidth(borderWidth);
        borderPaint.setColor(borderColor);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        updateCirclePath(w, h);
    }

    private void updateCirclePath(int width, int height) {
        circlePath.reset();
        float radius = Math.min(width, height) / 2f - borderWidth;
        circlePath.addCircle(width / 2f, height / 2f, radius, Path.Direction.CW);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        // 保存画布状态
        int saveCount = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null);
        }

        // 先绘制子视图（PreviewView）
        super.dispatchDraw(canvas);

        // 应用圆形遮罩
        canvas.drawPath(circlePath, maskPaint);

        // 绘制边框
        canvas.drawPath(circlePath, borderPaint);

        // 恢复画布
        canvas.restoreToCount(saveCount);
    }

    public PreviewView getPreviewView() {
        return previewView;
    }

    public void setBorder(int width, int color) {
        this.borderWidth = width;
        this.borderColor = color;
        borderPaint.setStrokeWidth(width);
        borderPaint.setColor(color);
        invalidate();
    }
}
